package com.songhuan.web.controller;

import com.songhuan.web.dto.AppUserCheckStat;
import com.songhuan.web.dto.CheckRecordDto;
import com.songhuan.web.dto.CheckStatusInfo;
import com.songhuan.web.dto.query.CheckRecordPagedInput;
import com.songhuan.web.entity.CheckRecord;
import com.songhuan.web.entity.CheckSetting;
import com.songhuan.web.mapper.CheckRecordMapper;
import com.songhuan.web.service.CheckRecordService;
import com.songhuan.web.tools.dto.IdInput;
import com.songhuan.web.tools.dto.IdsInput;
import com.songhuan.web.tools.dto.PagedResult;
import com.songhuan.web.tools.dto.ResponseData;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 打卡记录控制器
 */
@RestController()
@RequestMapping("/CheckRecord")
public class CheckRecordController {
    @Autowired()
    private CheckRecordService _CheckRecordService;
    @Autowired()
    private CheckRecordMapper _CheckRecordMapper;

    /**
     * 打卡记录分页查询
     */
    @RequestMapping(value = "/List", method = RequestMethod.POST)
    @SneakyThrows
    public PagedResult<CheckRecordDto> List(@RequestBody CheckRecordPagedInput input) {
        return _CheckRecordService.List(input);
    }

    /**
     * 单个打卡记录查询接口
     */
    @RequestMapping(value = "/Get", method = RequestMethod.POST)
    @SneakyThrows
    public CheckRecordDto Get(@RequestBody CheckRecordPagedInput input) {

        return _CheckRecordService.Get(input);
    }

    /**
     * 打卡记录创建或则修改
     */
    @RequestMapping(value = "/CreateOrEdit", method = RequestMethod.POST)
    public CheckRecordDto CreateOrEdit(@RequestBody CheckRecordDto input) throws Exception {
        return _CheckRecordService.CreateOrEdit(input);
    }

    /**
     * 打卡记录删除
     */
    @RequestMapping(value = "/Delete", method = RequestMethod.POST)
    public void Delete(@RequestBody IdInput input) {
        _CheckRecordService.removeById(input.getId());
    }

    /**
     * 打卡记录批量删除
     */
    @RequestMapping(value = "/BatchDelete", method = RequestMethod.POST)
    public void BatchDelete(@RequestBody IdsInput input) {
        _CheckRecordService.removeByIds(input.getIds());
    }

    /**
     * 检测我的是否满足打卡的条件
     */
    @RequestMapping(value = "/CheckMyStatus", method = RequestMethod.POST)
    public void CheckMyStatus(@RequestBody CheckRecordDto input) {
        _CheckRecordService.CheckMyStatus(input);
    }

    /**
     * 得到今天打卡情况
     *
     * @param input
     * @return
     */
    @RequestMapping(value = "/GetClockInfo", method = RequestMethod.POST)
    public CheckStatusInfo GetClockInfo(@RequestBody CheckRecordDto input) {
        return _CheckRecordService.GetClockInfo(input);
    }

    /**
     * 检测我的是否满足打卡的条件
     */
    @RequestMapping(value = "/Check", method = RequestMethod.POST)
    public void Check(@RequestBody CheckRecordDto input) {
        _CheckRecordService.Check(input);
    }

    /**
     * 抓取考勤信息
     */
    @PostMapping("/PullCheckSetting")
    public ResponseData<CheckSetting> PullCheckSetting(@RequestBody CheckRecordDto input) {

        CheckSetting checkSetting = _CheckRecordService.pullCheckSetting(input);
        return ResponseData.GetResponseDataInstance(checkSetting, "抓取成功", true);
    }

    @PostMapping("/ChangeCheckStatus")
    public ResponseData ChangeCheckStatus(@RequestBody AppUserCheckStat input) {

        _CheckRecordService.ChangeCheckStatus(input);
        return ResponseData.OfSuccess();
    }


    /**
     * 得到我的打卡记录
     */
    @RequestMapping(value = "/GetCheckList", method = RequestMethod.POST)
    public List<Map<String, Object>> GetCheckList(@RequestBody CheckRecordDto input) {
        return _CheckRecordService.GetCheckList(input);

    }

    /**
     * 得到我需要补卡的记录
     */
    @RequestMapping(value = "/GetNeedRepList", method = RequestMethod.POST)
    public List<Map<String, Object>> GetNeedRepList(@RequestBody CheckRecordDto input) {
        return _CheckRecordService.GetNeedRepList(input);
    }

    /**
     * 考勤按天统计最近30打卡人数
     */
    @RequestMapping(value = "/CheckRecordStatistics", method = RequestMethod.POST)
    public List<Map<String, Object>> CheckRecordStatistics(@RequestBody CheckRecordDto input) {
        List<Map<String, Object>> dataList = new ArrayList<>();
        List<CheckRecord> list = _CheckRecordMapper.selectList(null);

        for (int i = 0; i < 30; i++) {
            int day = i;
            Map<String, Object> data1 = new HashMap<>();
            data1.put("name", LocalDate.now().minusDays(i).toString());
            long normalcount = list.stream().filter(x ->  x.getIsLate()==null && x.getCreationTime().toLocalDate().equals(LocalDate.now().minusDays(day))).count();
            data1.put("value", normalcount);
            //迟到打卡
            long latecount = list.stream().filter(x ->x.getIsLate()!=null&& x.getIsLate() == true && x.getCreationTime().toLocalDate().equals(LocalDate.now().minusDays(day))).count();
            data1.put("latevalue", latecount);
            dataList.add(data1);

        }
        return dataList;


    }
}
